<!--
 * @Author: liuzilong 1174782213@qq.com
 * @Date: 2023-11-06 10:55:45
 * @LastEditors: liuzilong 1174782213@qq.com
 * @LastEditTime: 2023-11-08 15:06:05
 * @FilePath: \bunny-shop\src\pages\index\index.vue
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
-->
<script setup lang="ts">
import CustomNavbar from '@/pages/index/components/CustomNavbar.vue'
import PageSkeleton from '@/pages/index/components/PageSkeleton.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import { useGuessList } from '@/composables'

//获取轮播图数据
const list = ref<BannerItem[]>([])
const getList = async () => {
  const res = await getHomeBannerAPI()
  list.value = res.result
}
//获取分类数据
const categoryList = ref<CategoryItem[]>([])
const getCategoryList = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}
//获取热门数据
const hotList = ref<HotItem[]>([])
const getHotList = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}

const { guessRef, onScrolltolower } = useGuessList()
//下拉刷新
const triggered = ref(false)
const onrefresherrefresh: UniHelper.ScrollViewOnRefresherrefresh = async () => {
  triggered.value = true
  //先将猜你喜欢的页码置1
  guessRef.value?.reset()
  await Promise.all([getList(), getCategoryList(), getHotList(), guessRef.value?.getMore()])
  triggered.value = false
}
//骨架屏
const isLoading = ref(false)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getList(), getCategoryList(), getHotList()])
  isLoading.value = false
})
</script>

<template>
  <CustomNavbar></CustomNavbar>

  <scroll-view
    scroll-y
    class="scroll-view"
    @scrolltolower="onScrolltolower"
    refresher-enabled
    @refresherrefresh="onrefresherrefresh"
    :refresher-triggered="triggered"
  >
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <!-- 自定义导航栏 -->
      <XtxSwiper :list="list" />
      <!-- 分类面板 -->
      <CategoryPanel :categoryList="categoryList" />
      <!-- 热门推荐 -->
      <HotPanel :hotList="hotList" />
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss" scoped>
//
page {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
@/types/components
